package de.pixelate.flixelprimer
{
	import org.flixel.*;
	
	public class PlayState extends FlxState
	{
		[Embed(source="../../../../assets/mp3/ExplosionShip.mp3")]
		private var SoundExplosionShip:Class;
		[Embed(source="../../../../assets/mp3/ExplosionAlien.mp3")]
		private var SoundExplosionAlien:Class;
		[Embed(source="../../../../assets/mp3/Bullet.mp3")]
		private var SoundBullet:Class;
		[Embed(source="../../../../assets/mp3/StarWarsGameStart.mp3")]
		private var SoundLoop:Class;
		
		[Embed(source="../../../../assets/fonts/Arial.ttf", fontFamily = "Arial")] 
		private var FntArial:String;

		
		private var _scoreText: FlxText;
		private var _gameOverText: FlxText;
		private var _ship: Ship;
		private var _aliens: FlxGroup;	
		private var _bullets: FlxGroup;
		private var _spawnTimer: Number;
		private var _spawnInterval: Number = 2.5;
		private var aliens:Array = [Alien, Alien2, Alien3];
		private var _ammoIco:AmmoIco;
		private var _liveIco:LiveIco;
/*		private var _spaceBg:Stageback*/

		
		
			
		
		override public function create():void
		{
			
			bgColor = 0xF919191;
			
/*			_spaceBg = new Class();
			add(_spaceBg);*/
	
			_ship = new Ship();
			add(_ship);
			
			_aliens = new FlxGroup();
			add(_aliens);

			_bullets = new FlxGroup();
			add(_bullets);
			
			_ammoIco = new AmmoIco(700, 20);
			add(_ammoIco);
			
			_liveIco = new LiveIco(750, 20);
			add(_liveIco);
			
			FlxG.score = 0;
			_scoreText = new FlxText(10, 8, 200, "0");
			_scoreText.setFormat(null, 32, 0xF0066d1, "left");
			add(_scoreText);
			
					
			resetSpawnTimer();
			

			FlxG.playMusic(SoundLoop);

		
			super.create();
		}
		
		override public function update():void
		{			
			if(FlxG.keys.ENTER && _ship.dead)
			{
				FlxG.state = new PlayState();
			}			
			
			if(FlxG.keys.justPressed("SPACE") && _ship.dead == false)
			{
				spawnBullet(_ship.getBulletSpawnPosition(33));
				spawnBullet(_ship.getBulletSpawnPosition(-33));

				
			}
			
			_spawnTimer -= FlxG.elapsed;
			
			if(_spawnTimer < 0)
			{
				spawnAlien();
				resetSpawnTimer();
			}
			FlxU.overlap(_aliens, _bullets, overlapAlienBullet);
			FlxU.overlap(_aliens, _ship, overlapAlienShip);
					
			super.update();

		}
		
		private function spawnBullet(p: FlxPoint):void
		{
				var bullet: Bullet = new Bullet(p.x, p.y);
				_bullets.add(bullet);
				FlxG.play(SoundBullet);

		}
				
		private function spawnAlien():void
		{
			var x: Number = FlxG.width;
			var y: Number = Math.random() * (FlxG.height - 100) + 50;
			
	/*		var alien:FlxSprite = Math.random() < 0.5 ? new Alien(x, y) : new Alien3(x, y);
			_aliens.add(alien);*/
			
			// find out the index in the array
			var min:int = 0;
			var max:int = aliens.length - 1;
			var index:int = Math.floor(Math.random() * (max - min + 1)) + min;
			
			// get the class from the array
			var alienClass:Class = aliens[index];
			
			// create a new instance from the class
			var alien:FlxSprite = new alienClass(x, y); // new Alien(x, y) || new Alien2(x, y) || new Alien3(x, y)
			
			// add the new instance to the _aliens FlxGroup
			_aliens.add(alien);
		}

		private function resetSpawnTimer():void
		{
			_spawnTimer = _spawnInterval;
			_spawnInterval *= 0.95;
			if(_spawnInterval < 0.1)
			{
				_spawnInterval = 0.1;
			}		
		}
		
		private function overlapAlienBullet(alien: FlxSprite, bullet: Bullet):void
		{
			alien.kill();
			bullet.kill();
			FlxG.score += 1;
			_scoreText.text = FlxG.score.toString();
			FlxG.play(SoundExplosionAlien);
			var emitter:FlxEmitter = createEmitter();
			emitter.at(alien);
		}


		
		private function overlapAlienShip(alien: FlxSprite, ship: Ship):void
		{
			ship.kill();
			alien.kill();
			FlxG.quake.start(0.02);
			var emitter:FlxEmitter = createEmitter();
			emitter.at(ship);
			
			_gameOverText = new FlxText(0, FlxG.height / 2, FlxG.width,
				"GAME OVER\n\nYOU LOSER\nPRESS ENTER TO PLAY AGAIN");
			_gameOverText.setFormat("Arial", 16, 0x0066d1, "center");
			add(_gameOverText);
			FlxG.play(SoundExplosionShip);
		}
		
		private function createEmitter():FlxEmitter
		{
			var emitter:FlxEmitter = new FlxEmitter();
			emitter.delay = 1;
			emitter.gravity = 0;
			emitter.maxRotation = 0;
			emitter.setXSpeed(-500, 500);
			emitter.setYSpeed(-500, 500);
			var particles: int = 10;
			for(var i: int = 0; i < particles; i++)
			{
				var particle:FlxSprite = new FlxSprite();
				particle.createGraphic(2, 2, 0xFF0066d1);
				particle.exists = false;
				emitter.add(particle);
			}
			emitter.start();
			add(emitter);
			return emitter;
		}
	}
}